package com.xkdx.guangguang.fragment.user;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;

public class UserQuickRegistModule extends AbsModule {
	public User user;
	public DetailInfo info;
	JSONObject values;
	private Context context;

	private UserSharePrefence user_sp;
	JSONArray jsonArray;
	JSONObject jsonObj;

	public UserQuickRegistModule(Context context) {
		this.context = context;
	}

	@Override
	public void parseData() throws Exception {

		try {
			user = new User();
			info = new DetailInfo();
			jsonArray = (JSONArray) result;

			for (int i = 0; i < jsonArray.length(); i++) {
				jsonObj = (JSONObject) jsonArray.get(i);
				String sortOfAct = jsonObj.getString("Act");
				if (sortOfAct.equals("register")) {
					user = parserUserRgist(jsonObj.getJSONObject("Content"));
					if (user.getStatus().equals("Error")) {
						continue;
					} else {
						break;
					}
				} else {
					user = parserUserRgist(jsonObj.getJSONObject("Content"));
				}

			}
		} catch (Exception e) {
			throw e;
		}

	}

	private User parserUserRgist(JSONObject userRigist) throws Exception {

		try {
			JSONObject stut = (JSONObject) userRigist.get("Result");

			user.setErrorCode(stut.getString("ErrorCode"));
			user.setMessage(stut.getString("Message"));
			user.setType(stut.getString("Type"));
			user.setStatus(stut.getString("Status"));

			if (stut.has("DetailInfo")) {
				JSONObject detail = (JSONObject) stut
						.getJSONObject("DetailInfo");

				info.setSex(detail.getString("Sex"));
				info.setBirthday(detail.getString("Birthday"));
				info.setEmail(detail.getString("Email"));
				info.setEmailVerified(detail.getString("EmailVerified"));
				info.setMobilePhone("");
				info.setMobilePhoneVerified(detail
						.getString("MobilePhoneVerified"));
				info.setUserID(detail.getString("UserID"));
				info.setLoginToken(detail.getString("LoginToken"));
				info.setNickName(detail.getString("NickName"));
				info.setPictureUrl(detail.getString("PictureUrl"));
				info.setPoint(detail.getString("Point"));

				user.setDetailInfo(info);

				// 将用户信息存到本地
				if ("Success".equals(stut.getString("Status"))) {
					user_sp = new UserSharePrefence(context, IConstants.SP_USER);
					user_sp.setUserBirthday(detail.getString("Birthday"));
					// Log.i("UserBirthday", user_sp.getUserBirthday());
					user_sp.setUserEmail(detail.getString("Email"));
					user_sp.setUserEmailVerified(detail
							.getString("EmailVerified"));
					user_sp.setUserSex(detail.getString("Sex"));
					user_sp.setUserID(detail.getString("UserID"));
					user_sp.setUserMessage(stut.getString("Message"));
					user_sp.setUserStatus(stut.getString("Status"));
					user_sp.setUserMobilePhone("");
					user_sp.setUserMobilePhoneVerified(detail
							.getString("MobilePhoneVerified"));
					user_sp.setUserErrorCode(stut.getString("ErrorCode"));
					user_sp.setUserType(stut.getString("Type"));
					user_sp.setUserNickName(detail.getString("NickName"));
					user_sp.setUserPictureUrl(detail.getString("PictureUrl"));
					// 设置令牌
					user_sp.setUserLoginToken(detail.getString("LoginToken"));

					if (detail.has("SinaWeiboUserID")) {
						user_sp.setSinaWeiboUserID(detail
								.getString("SinaWeiboUserID"));
					}
					if (detail.has("SinaWeiboNickName")) {
						user_sp.setSinaWeiboNickName(detail
								.getString("SinaWeiboNickName"));
					}
					if (detail.has("SinaWeiboToken")) {
						user_sp.setSinaWeiboToken(detail
								.getString("SinaWeiboToken"));
					}
					if (detail.has("SinaWeiboExpireTime")) {
						user_sp.setSinaWeiboExpireTime(detail
								.getString("SinaWeiboExpireTime"));
					}
					if (detail.has("QQOpenID")) {
						user_sp.setQQOpenID(detail.getString("QQOpenID"));
					}
					if (detail.has("QQNickName")) {
						user_sp.setQQNickName(detail.getString("QQNickName"));
					}
					if (detail.has("QQToken")) {
						user_sp.setQQToken(detail.getString("QQToken"));
					}
					if (detail.has("QQExpireTime")) {
						user_sp.setQQExpireTime(detail
								.getString("QQExpireTime"));
					}
					if (detail.has("UserType")) {
						user_sp.setVerifyType(detail.getString("UserType"));
					}
					if (detail.has("Point")) {
						user_sp.setUserPoint(detail.getString("Point"));
					}
					if (detail.has("ShopIds")) {
						if (detail.getString("ShopIds") != null
								&& !("".equals(detail.getString("ShopIds")))) {
							user_sp.setShopIds(detail.getString("ShopIds"));
						} else {
							user_sp.setShopIds("");
						}
					}
				}
			}
		} catch (JSONException e) {
			throw e;
		}
		return user;

	}

	public class User {
		private String UserID;
		private String Type;
		private String Status;
		private String ErrorCode;
		private String Message;
		private DetailInfo DetailInfo;
		private String UpdateTime;

		public User() {
			super();
			DetailInfo = new DetailInfo();
		}

		public String getType() {
			return Type;
		}

		public void setType(String type) {
			Type = type;
		}

		public String getStatus() {
			return Status;
		}

		public void setStatus(String status) {
			Status = status;
		}

		public String getErrorCode() {
			return ErrorCode;
		}

		public void setErrorCode(String errorCode) {
			ErrorCode = errorCode;
		}

		public String getMessage() {
			return Message;
		}

		public void setMessage(String message) {
			Message = message;
		}

		public DetailInfo getDetailInfo() {
			return DetailInfo;
		}

		public void setDetailInfo(DetailInfo detailInfo) {
			DetailInfo = detailInfo;
		}

		public String getUserID() {
			return UserID;
		}

		public void setUserID(String userID) {
			UserID = userID;
		}

		public void setUpdateTime(String updateTime) {
			UpdateTime = updateTime;
		}

		public String getUpdateTime() {
			return UpdateTime;
		}

	}

	public class DetailInfo {
		private String UserID;
		private String NickName;
		private String Point;
		private String Sex;
		private String Birthday;
		private String PictureUrl;
		private String MobilePhone;
		private String mobilePhoneVerified;
		private String Email;
		private String EmailVerified;
		private String SessionID;
		private String LoginToken;

		private String SinaWeiboUserID;
		private String SinaWeiboNickName;
		private String SinaWeiboToken;
		private String SinaWeiboExpireTime; // 增加sinaweibo的有效时期
		private String ShopIds;

		private String QQOpenID;
		private String QQToken;
		private String QQNickName;
		private String QQExpireTime;
		private String VerifyType; // 新增用户验证类型

		public String getUserID() {
			return UserID;
		}

		public void setUserID(String userID) {
			UserID = userID;
		}

		public String getNickName() {
			return NickName;
		}

		public void setNickName(String nickName) {
			NickName = nickName;
		}

		public String getPoint() {
			return Point;
		}

		public void setPoint(String point) {
			Point = point;
		}

		public String getSex() {
			return Sex;
		}

		public void setSex(String sex) {
			Sex = sex;
		}

		public String getBirthday() {
			return Birthday;
		}

		public void setBirthday(String birthday) {
			Birthday = birthday;
		}

		public String getPictureUrl() {
			return PictureUrl;
		}

		public void setPictureUrl(String pictureUrl) {
			PictureUrl = pictureUrl;
		}

		public String getMobilePhone() {
			return MobilePhone;
		}

		public void setMobilePhone(String mobilePhone) {
			MobilePhone = mobilePhone;
		}

		public String getMobilePhoneVerified() {
			return mobilePhoneVerified;
		}

		public void setMobilePhoneVerified(String mobilePhoneVerified) {
			this.mobilePhoneVerified = mobilePhoneVerified;
		}

		public String getEmail() {
			return Email;
		}

		public void setEmail(String email) {
			Email = email;
		}

		public String getEmailVerified() {
			return EmailVerified;
		}

		public void setEmailVerified(String emailVerified) {
			EmailVerified = emailVerified;
		}

		public String getSessionID() {
			return SessionID;
		}

		public void setSessionID(String sessionID) {
			SessionID = sessionID;
		}

		public String getLoginToken() {
			return LoginToken;
		}

		public void setLoginToken(String loginToken) {
			LoginToken = loginToken;
		}

		public void setSinaWeiboUserID(String sinaWeiboUserID) {
			SinaWeiboUserID = sinaWeiboUserID;
		}

		public String getSinaWeiboUserID() {
			return SinaWeiboUserID;
		}

		public void setSinaWeiboNickName(String sinaWeiboNickName) {
			SinaWeiboNickName = sinaWeiboNickName;
		}

		public String getSinaWeiboNickName() {
			return SinaWeiboNickName;
		}

		public void setSinaWeiboToken(String sinaWeiboToken) {
			SinaWeiboToken = sinaWeiboToken;
		}

		public String getSinaWeiboToken() {
			return SinaWeiboToken;
		}

		public void setQQOpenID(String qQOpenID) {
			QQOpenID = qQOpenID;
		}

		public String getQQOpenID() {
			return QQOpenID;
		}

		public void setQQNickName(String qQNickName) {
			QQNickName = qQNickName;
		}

		public String getQQNickName() {
			return QQNickName;
		}

		public void setQQToken(String qQToken) {
			QQToken = qQToken;
		}

		public String getQQToken() {
			return QQToken;
		}

		public void setSinaWeiboExpireTime(String sinaWeiboExpireTime) {
			SinaWeiboExpireTime = sinaWeiboExpireTime;
		}

		public String getSinaWeiboExpireTime() {
			return SinaWeiboExpireTime;
		}

		public void setQQExpireTime(String qQExpireTime) {
			QQExpireTime = qQExpireTime;
		}

		public String getQQExpireTime() {
			return QQExpireTime;
		}

		public void setVerifyType(String verifyType) {
			VerifyType = verifyType;
		}

		public String getVerifyType() {
			return VerifyType;
		}

		public String getShopIds() {
			return ShopIds;
		}

		public void setShopIds(String shopIds) {
			ShopIds = shopIds;
		}

	}

}
